/* 
 * Copyright (c), nFractals Ltd. All Rights Reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.parade.distributed;

import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;

import org.parade.Parade;
import org.parade.driver.Driver;

public class Queue<E> implements java.util.concurrent.BlockingQueue<E>, Object{
	private Driver driver;
	private Id id;
	private java.util.concurrent.BlockingQueue queue;

	/**
	 * Create queue using driver provided by singleton Parade object
	 * 
	 * @param id		Cluster wide unique object id
	 */
	public Queue(Id id){
		this.driver = Parade.getDriver();
		this.id = id;
	}
	
	/**
	 * Create queue using given driver
	 * 
	 * @param driver	Distributed driver to use
	 * @param id		Cluster wide unique object id
	 */
	public Queue(Driver driver, Id id){
		this.driver = driver;
		this.id = id;
		this.queue = driver.getBlockingQueue(id);
	}

	@Override
	public int drainTo(Collection<? super E> c) {
		return queue.drainTo(c);
	}

	@Override
	public int drainTo(Collection<? super E> c, int maxElements) {
		return queue.drainTo(c, maxElements);
	}

	@Override
	public int remainingCapacity() {
		return queue.remainingCapacity();
	}

	@Override
	public boolean addAll(Collection<? extends E> c) {
		return queue.addAll(c);
	}

	@Override
	public void clear() {
		queue.clear();
	}

	@Override
	public boolean containsAll(Collection<?> c) {
		return queue.containsAll(c);
	}

	@Override
	public boolean isEmpty() {
		return queue.isEmpty();
	}

	@Override
	public boolean removeAll(Collection<?> c) {
		return queue.removeAll(c);
	}

	@Override
	public boolean retainAll(Collection<?> c) {
		return queue.retainAll(c);
	}

	@Override
	public java.lang.Object[] toArray() {
		return queue.toArray();
	}

	@Override
	public <T> T[] toArray(T[] a) {
		return (T[]) queue.toArray(a);
	}

	@Override
	public E element() {
		return (E) queue.element();
	}

	@Override
	public E peek() {
		return (E) queue.peek();
	}

	@Override
	public E poll() {
		return (E) queue.poll();
	}

	@Override
	public E remove() {
		return (E) queue.remove();
	}

	@Override
	public Iterator<E> iterator() {
		return queue.iterator();
	}

	@Override
	public int size() {
		return queue.size();
	}

	@Override
	public boolean add(E e) {
		return queue.add(e);
	}

	@Override
	public boolean contains(java.lang.Object o) {
		return queue.contains(o);
	}

	@Override
	public boolean offer(E e) {
		return queue.offer(e);
	}

	@Override
	public boolean offer(E e, long timeout, TimeUnit unit)
			throws InterruptedException {
		return queue.offer(e, timeout, unit);
	}

	@Override
	public E poll(long timeout, TimeUnit unit) throws InterruptedException {
		return (E) queue.poll(timeout, unit);
	}

	@Override
	public void put(E e) throws InterruptedException {
		queue.put(e);
	}

	@Override
	public boolean remove(java.lang.Object o) {
		return queue.remove(o);
	}

	@Override
	public E take() throws InterruptedException {
		return (E) queue.take();
	}

	@Override
	public Id getDistributedObjectId() {
		return id;
	}
}